package eje;

//import robocode.HitRobotEvent;
import java.awt.Color;
import robocode.BulletHitEvent;
import robocode.HitByBulletEvent;
import robocode.HitRobotEvent;
import robocode.HitWallEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
import static robocode.util.Utils.normalRelativeAngleDegrees;


/*
 * Author: Jesse Elfalan
 * Robocode competitive robot, BumbleBee
 * description: tracks and attacks enemy robot and moves only when necessary to avoid
 * enemy fire
 */



public class BumbleBee extends Robot {

  String trackBot;
  double gunTurn = 0;
  int count = 0;
  
  public boolean tracking = false;
  public ScannedRobotEvent onTarget;

  public double bearingFromGun = 0;
  public double turnDir = 0;
  
  public double absoluteBearing = 0; 
  
  public void run() {
    
    //color scheme
    setBodyColor(Color.yellow);
    setBulletColor(Color.red);
    setGunColor(Color.black);
    setRadarColor(Color.gray);
    setScanColor(Color.yellow);
    

  while (true) {
    // turn the Gun to look for an enemy
    turnGunRight(gunTurn);
  
    count++;
    // after two turns with no success of finding an enemy, look left
    if (count > 2) {
      gunTurn = -10;
    }
   // after two turns with no success of finding an enemy, look right
    if (count > 5) {
      gunTurn = 10;
    }
    // after ten turns, start over and find another target
    if (count > 11) {
      trackBot = null;
    }
   }
  }

  public void dodge(){

  ahead(getHeading());
  fire(1);
  back(getHeading());
  }

   public void onHitRobot(HitRobotEvent e){
     if (onTarget.getBearing() >= 0) {
       turnDir = 1;
     } else {
       turnDir = -1;
     }

     turnRight(e.getBearing());
     ahead(onTarget.getDistance() + 5);
     adjustGun(onTarget);
     scan();
    }
  
  public void onHitByBullet(HitByBulletEvent e){
    turnRight(90);
    back(100);
    if (onTarget.getBearing() >= 0) {
      turnDir = 10;
    } else {
      turnDir = -10;
    }

    turnRight(e.getBearing());
    back(onTarget.getDistance() + 5);
    adjustGun(onTarget);
    scan();
  }

  public void onHitByWall(HitWallEvent e){
    turnRight(180);
    back(100);
    adjustGun(onTarget);
  }

  
  public void adjustGun(ScannedRobotEvent target){
    bearingFromGun = normalRelativeAngleDegrees(absoluteBearing - getGunHeading());
    gunTurn = normalRelativeAngleDegrees(target.getBearing() + (getHeading() - getRadarHeading()));
    turnGunRight(gunTurn);
    //fire(Math.min(3 - Math.abs(bearingFromGun), getEnergy() - .1));
    fire(Math.min(3 - Math.abs(bearingFromGun), getEnergy() - .1));
    
    scan();
    
  }
  public void onScannedRobot(ScannedRobotEvent e) {
    //used to calculate the bullet strength
    absoluteBearing = getHeading() + e.getBearing();
    bearingFromGun = normalRelativeAngleDegrees(absoluteBearing - getGunHeading());
    onTarget = e;
  //if no target is found, break from method and return to main
    if (trackBot != null && !e.getName().equals(trackBot)) {
      return;
    }

  // found an enemy to target
    if (trackBot == null) {
      trackBot = e.getName();
      tracking = true;
      ahead(e.getDistance()+5);
         adjustGun(onTarget);
         scan();
       
          }
  
    // Reset count to hone in on target
    count = 0;

  //adjust turret to track enemy's movement and adjust fire strength according to distance
    gunTurn = normalRelativeAngleDegrees(e.getBearing() + (getHeading() - getRadarHeading()));
    turnGunRight(gunTurn);
    fire(Math.min(3 - Math.abs(bearingFromGun), getEnergy() - .1));
    scan();
  }
}

